<?php

/**
 * Classe para manipulação de imagens
 */
class GImage {

    private $imgInput;
    private $imgOutput;
    private $imgSrc;
    private $format;
    private $quality = 80;
    private $xInput;
    private $yInput;
    private $xOutput;
    private $yOutput;
    private $resize;

    public function setImg($img) {
        $ext = strtoupper(pathinfo($img, PATHINFO_EXTENSION));

        if (is_file($img) && ($ext == "JPG" || $ext == "JPEG")) {
            $this->format = $ext;
            $this->imgInput = ImageCreateFromJPEG($img);
            $this->imgSrc = $img;
        } else if (is_file($img) && $ext == "PNG") {
            $this->format = $ext;
            $this->imgInput = ImageCreateFromPNG($img);
            $this->imgSrc = $img;
        } else if (is_file($img) && $ext == "GIF") {
            $this->format = $ext;
            $this->imgInput = ImageCreateFromGIF($img);
            $this->imgSrc = $img;
        }

        $this->xInput = imagesx($this->imgInput);
        $this->yInput = imagesy($this->imgInput);
    }

    public function setSize($x_size = 100, $ySize = 100, $wide = true) {
        if ($wide) {
            if ($this->xInput >= $this->yInput) {
                $this->xOutput = $xSize;
                $this->yOutput = ($this->xOutput / $this->xInput) * $this->yInput;
            } else {
                $this->yOutput = $ySize;
                $this->xOutput = ($this->yOutput / $this->yInput) * $this->xInput;
            }
        } else {
            $this->xOutput = $xSize;
            $this->yOutput = $ySize;
        }
        $this->resize = TRUE;
    }

    public function setQuality($quality) {
        if (is_int($quality)) {
            $this->quality = $quality;
        }
    }

    public function saveImg($path, $x, $y, $w, $h) {
        $this->xInput = $w;
        $this->yInput = $h;

        if ($this->resize) {
            $this->imgOutput = ImageCreateTrueColor($this->xOutput, $this->yOutput);
            if ($this->format == "PNG") {
                imagealphablending($this->imgOutput, false);
                imagesavealpha($this->imgOutput, true);
                $source = imagecreatefrompng($this->imgSrc);
                imagealphablending($source, true);
                ImageCopyResampled($this->imgOutput, $source, 0, 0, $x, $y, $this->xOutput, $this->yOutput, $this->xInput, $this->yInput);
            } else
                ImageCopyResampled($this->imgOutput, $this->imgInput, 0, 0, $x, $y, $this->xOutput, $this->yOutput, $this->xInput, $this->yInput);
        }

        if ($this->format == "JPG" OR $this->format == "JPEG") {
            if ($this->resize) {
                imageJPEG($this->imgOutput, $path, $this->quality);
            } else {
                copy($this->imgSrc, $path);
            }
        } else if ($this->format == "PNG") {
            if ($this->resize) {
                imagePNG($this->imgOutput, $path);
            } else {
                copy($this->imgSrc, $path);
            }
        } else if ($this->format == "GIF") {
            if ($this->resize) {
                imageGIF($this->imgOutput, $path);
            } else {
                copy($this->imgSrc, $path);
            }
        }
    }

    public function getWidth() {
        return $this->xInput;
    }

    public function getHeight() {
        return $this->yInput;
    }

    public function clearCache() {
        @ImageDestroy($this->imgInput);
        @ImageDestroy($this->imgOutput);
    }

}

?>